<!--
 * @Author: hq
 * @Date: 2022-05-24 10:21:11
 * @LastEditors: hq
 * @LastEditTime: 2022-08-10 14:37:41
 * @Description: 编辑弹窗
 * @version: 1.0
-->
<template>
  <div class="dialog">
    <div class="content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="选择上级节点">
          <el-select
            ref="selectTree"
            v-model="ruleForm.tree_id"
            placeholder="请选择..."
          >
            <el-option
              :key="select_id"
              :value="select_id"
              :label="select_title"
              style="height: auto"
              hidden
            />
            <el-tree
              ref="trees"
              :data="treeData"
              :props="defaultProps"
              node-key="id"
              highlight-current
              :current-node-key="current"
              :default-expand-all="false"
              :expand-on-click-node="false"
              @node-click="handleClickNode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="节点名称" prop="title">
          <el-input
            v-model="ruleForm.title"
            placeholder="请输入节点名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="节点图标" prop="icon">
          <el-input
            v-model="ruleForm.icon"
            placeholder="请输入节点图标"
          ></el-input>
        </el-form-item>
        <el-form-item label="方法" prop="action">
          <el-input
            v-model="ruleForm.action"
            placeholder="请输入路径"
          ></el-input>
        </el-form-item>
        <el-form-item label="路由" prop="router_addr">
          <el-input
            v-model="ruleForm.router_addr"
            placeholder="请输入路由"
          ></el-input>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input
            type="number"
            v-model="ruleForm.sort"
            placeholder="请输入排序"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属模块" prop="model_type">
          <el-select v-model="ruleForm.model_type" placeholder="请选择">
            <el-option
              v-for="item in modelArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-select v-model="ruleForm.position" placeholder="请选择">
            <el-option
              v-for="item in positionArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :required="true">
          <el-switch
            active-text="禁用"
            inactive-text="启用"
            :active-value="1"
            :inactive-value="0"
            class="tablescope"
            v-model="ruleForm.status"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="是否显示" :required="true">
          <el-switch
            active-text="隐藏"
            inactive-text="显示"
            :active-value="1"
            :inactive-value="0"
            class="tablescope"
            v-model="ruleForm.hide"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="是否公共使用" :required="true">
          <el-switch
            active-text="否"
            inactive-text="是"
            :active-value="1"
            :inactive-value="0"
            class="tablescope"
            v-model="ruleForm.pub"
          >
          </el-switch>
        </el-form-item>
      </el-form>
    </div>

    <div class="btn">
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { menuList, doMenu, menuDetail } from "@/api/manager";
export default {
  props: ["rowData"],
  data() {
    return {
      current: "",
      treeData: [{ id: 0, title: "顶级节点" }],
      treeList: [],
      defaultProps: {
        /** 唯一标识 **/
        value: "id",
        /** 标签显示 **/
        label: "title",
        /** 子级 **/
        children: "children",
      },
      //这个是el-select要绑定的数据，等下需要赋值
      select_id: "",
      select_title: "",
      ruleForm: {
        tree_id: "",
        title: "",
        icon: "",
        action: "",
        router_addr: "",
        sort: "",
        model_type: 1,
        position: "list",
        status: 1,
        hide: 1,
        pub: 1,
      },
      modelArr: [
        {
          id: 1,
          name: "系统",
        },
      ],
      positionArr: [
        {
          id: "list",
          name: "list",
        },
        {
          id: "top",
          name: "top",
        },
        {
          id: "right",
          name: "right",
        },
      ],
      rules: {
        title: [{ required: true, message: "请输入节点名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    async menuList() {
      const { data, err } = await menuList({});
      if (err === 0) {
        this.treeData = this.treeData.concat(data);
        //编辑
        if (this.rowData.id) {
          this.menuDetail();
        }
      }
    },
    async menuDetail() {
      const { data, err } = await menuDetail({
        id: this.rowData.id,
      });
      if (err === 0) {
        this.ruleForm.tree_id = data.pid;
        this.select_id = data.pid;
        this.select_title = data.pid_text;
        this.$nextTick(() => {
          this.current = data.pid;
        });
        this.ruleForm.title = data.title;
        this.ruleForm.icon = data.icon;
        this.ruleForm.action = data.action;
        this.ruleForm.router_addr = data.route;
        this.ruleForm.sort = data.sort;
        this.ruleForm.position = data.position;
        this.ruleForm.status = data.status;
        this.ruleForm.hide = data.hide;
        this.ruleForm.pub = data.pub;
      }
    },
    //操作
    async doMenu(action_type) {
      const { msg, err } = await doMenu({
        id: this.rowData.id,
        pid: this.ruleForm.tree_id,
        module: this.ruleForm.model_type,
        title: this.ruleForm.title,
        icon: this.ruleForm.icon,
        action: this.ruleForm.action,
        route: this.ruleForm.router_addr,
        sort: this.ruleForm.sort,
        position: this.ruleForm.model_type,
        status: this.ruleForm.status,
        hide: this.ruleForm.hide,
        pub: this.ruleForm.pub,
        action_type: action_type,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功！",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    handleSubmit() {
      if (this.rowData.id) {
        //编辑
        this.doMenu("MOD");
      } else {
        //新增
        this.doMenu("ADD");
      }
    },
    //节点点击事件
    handleClickNode(data) {
      this.ruleForm.tree_id = data.id;
      this.select_id = data.id;
      this.select_title = data.title;
      // 选择器执行完成后，使其失去焦点隐藏下拉框的效果
      this.$refs.selectTree.blur();
    },
    // dataFilter(val) {
    //   console.log(val);
    //   this.$refs.trees.filter(val);
    // },
  },
  created() {
    this.menuList();
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 20px;
  box-sizing: border-box;
  height: 100%;
  .content {
    height: 92%;
    overflow: auto;
    &::-webkit-scrollbar {
      width: 0;
    }
  }
  .row {
    display: flex;
    margin-bottom: 20px;
    .col {
      width: 100px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      flex-shrink: 0;
      margin-right: 10px;
    }
    .red {
      margin-right: 5px;
    }
  }
  .row:nth-child(3) {
    align-items: flex-start;
  }
  .el-input__inner {
    height: 36px;
    line-height: 36px;
  }
  //   .btn {
  //     box-sizing: border-box;
  //     .el-button {
  //       width: 100%;
  //     }
  //   }
}
/deep/.el-tree--highlight-current
  .el-tree-node.is-current
  > .el-tree-node__content {
  .el-tree-node__label {
    color: #2978ff !important;
  }
}
</style>
